﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 第六次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = openFileDialog1.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                //array存储选中文件的文件名
                string filename = openFileDialog1.FileName;

                //如果文件名不为空，则将文件打开，显示到文本框上
                if (!string.IsNullOrEmpty(filename))
                {
                    StreamReader sr = new StreamReader(filename);
                    textBox4.Text = filename;
                    textBox1.Text = sr.ReadToEnd();
                    sr.Close();
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = openFileDialog1.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                //array存储选中文件的文件名
                string filename = openFileDialog1.FileName;

                //如果文件名不为空，则将文件打开，显示到文本框上
                if (!string.IsNullOrEmpty(filename))
                {
                    StreamReader sr = new StreamReader(filename);
                    textBox5.Text = filename;
                    textBox2.Text = sr.ReadToEnd();
                    sr.Close();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //创建路径
            string path = System.Windows.Forms.Application.StartupPath + "\\Data\\";
            //如果文件夹不存在，新建文件夹
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            //创建目标文件
            string postPath = path + "target" + ".txt";
            FileStream fileStream = new FileStream(postPath, FileMode.Create);
            FileStream[] fileStreams = new FileStream[2];

            int b;
            fileStreams[0] = new FileStream(textBox4.Text, FileMode.Open);
            while ((b = fileStreams[0].ReadByte()) != -1)
                fileStream.WriteByte((byte)b);
            

            fileStreams[1] = new FileStream(textBox5.Text, FileMode.Open);
            while ((b = fileStreams[1].ReadByte()) != -1)
                fileStream.WriteByte((byte)b);
            
            fileStream.Close();

            textBox3.Text = File.ReadAllText(postPath);
        }
    }
}
